<?php
/**
 * SQLite示例
 * User: ford
 * Date: 15-6-23
 * Time: 下午1:28
 */
$ddl = <<<EOT
CREATE TABLE dept (
	deptno integer NOT NULL,
	dname text,
	loc text,
	CONSTRAINT dept_pkey PRIMARY KEY(deptno)
);

CREATE TABLE emp (
	empno integer NOT NULL,
	ename text,
	job text,
	mgr integer,
	hiredate text,
	sal real,
	comm real,
	deptno integer,
	CONSTRAINT emp_pkey PRIMARY KEY(empno),
	CONSTRAINT fk_deptno FOREIGN KEY(deptno)
		REFERENCES dept(deptno) ON DELETE CASCADE
);

CREATE UNIQUE INDEX pk_emp ON emp(empno);
CREATE INDEX emp_deptno ON emp(deptno);
CREATE UNIQUE INDEX pk_dept ON dept(deptno);
EOT;

try{
	$db = new SQLite3("scott.sqlite");	//这样创建一个SQLite数据库文件，简单方便
	@$db->exec($ddl);	//创建数据库结构
	if($db->lastErrorCode() != 0) {
		throw new Exception($db->lastErrorMsg() . "\n");
	}

	echo "Database structure created successfully.\n";
}
catch (Exception $e) {
	echo "Exception:\n";
	dir($e->getMessage());
}
